/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.superme.ResConsumerServiceByribbon.controller;




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@RestController
public class ResourcesHomeController {

	@Autowired
	OAuth2RestTemplate rest;

	//@Autowired
	//OAuth2ClientContext oauth2Context;


	//@PreAuthorize("#oauth2.hasScope('openid') and hasRole('ROLE_ADMIN')")
	@RequestMapping(value="/hello", method = RequestMethod.GET)
	@ResponseBody
	public String test(OAuth2Authentication oAuth2Authentication) {
		if (oAuth2Authentication != null) {


			//ClientCredentialsResourceDetails ccr =  new ClientCredentialsResourceDetails();
			//ccr.setClientId(oAuth2Authentication.getOAuth2Request().getClientId());
			//OAuth2RestTemplate rest = new OAuth2RestTemplate(ccr,oauth2Context);;
			String url = "http://gatewayserver:8765/res/";
			HttpHeaders headers = new HttpHeaders();
			HttpEntity entity =  new HttpEntity<String>(headers);
			HashMap<String,String> urivarmaps = new HashMap<String,String>();
			ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET,entity, String.class, urivarmaps);
			return  response.getBody();
		} else {
			return "anonymous";
		}

	}

}
